package com.ruoyi.system.controller;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.utils.IdUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.convert.RedisTypeMapper;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("aly")
public class LoginController {
    @Autowired
    private ISysUserService userService;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private RedisService redisService;

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 阿里云短信验证
     * @param
     * @return
     */
    @RequestMapping("alyGetCode")
    public AjaxResult alyGetCode(@RequestBody Map<String, String> map) {
        String phone = map.get("phone");
        //防止用户重复申请验证码
        Boolean phoneNum = redisTemplate.opsForValue().setIfAbsent(phone , "");
        if (!phoneNum){
            return AjaxResult.error("操作频繁，请稍后再试！");
        }
        //手机号未注册
        Map<String, String> userByPhone = userService.getUserByPhone(phone);
        if (StringUtils.isEmpty(userByPhone)) {
            return AjaxResult.error("当前手机号未注册");
        }
        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        //在请求头中添加AppCode
        headers.set("Authorization" , "APPCODE " + "011bcadb635d4e42a1e71dfb45229cc5");
        //设置请求参数
        MultiValueMap<String,String> params = new LinkedMultiValueMap<>();
        params.add("phone", phone);            //添加手机号
        params.add("templateId", "540");     //模板编号
        params.add("variables", "7748");     //自定义验证码
        // 保存验证码信息
        String uuid = IdUtils.simpleUUID();
        //生成redis key值
        String verifyKey = "phone_codes:" + uuid;

        //发送短信请求
//        ResponseEntity<Map> mapResponseEntity = restTemplate.postForEntity("https://aliapi.market.alicloudapi.com/smsApi/verifyCode/send", new HttpEntity<>(params, headers), Map.class);
        System.out.println("----------------------"+"短信验证码：7748");
//        Integer returnStatus = Integer.valueOf(mapResponseEntity.getBody().get("returnStatus").toString());
        int returnStatus = 1 ;
        //判断短信是否发送成功
        if (returnStatus == 1){
            //成功则将手机号、验证码存入redis中
            //将手机号存入redis
            redisService.setCacheObject(phone , "" , Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
            //短信验证码有效时间两分钟
            redisService.setCacheObject(verifyKey, "7748", Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
            return AjaxResult.success("短信发送成功").put("uuid" , uuid);
        }

        redisTemplate.delete(phone);
        return AjaxResult.error("短信发送失败，请稍后重试！");
    }

}
